/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ArchDemoFrame.java
 *
 * Created on 6-nov-2011, 20:07:31
 */
package nl.windesheim.archdemo.view;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import nl.windesheim.archdemo.presentationmodel.MainFramePresenter;

/**
 *
 * @author reindert
 */

// wishlist
// TODO: add filter on order list (by customer, by status)
// TODO: comments, documentation (UML)
// TODO: explanations about spring/hibernate
// TODO: resizing code (dont make app resizable at all?)
// TODO: remove auto-update from Hibernate (too slow on startup)
public class MainFrame extends javax.swing.JFrame implements Thread.UncaughtExceptionHandler {

    private final static Logger LOG = Logger.getLogger(MainFrame.class.getName());
    
    private MainFramePresenter presentation;
    
    public MainFrame(MainFramePresenter presentation){
        this.presentation = presentation;
        Thread.setDefaultUncaughtExceptionHandler(this);
        initComponents();

    }

  
    /** Gracefully handle exceptions. Note: any other exceptions before, during 
     or after display of the dialog may cause unwanted behaviour (endless loop of new dialogs etc.) */
    @Override
    public void uncaughtException(final Thread t, final Throwable e) {
        LOG.log(Level.SEVERE, null, e);
        if (SwingUtilities.isEventDispatchThread()) {
            String msg = String.format("Unexpected problem: %s", e.getMessage());
            JOptionPane.showMessageDialog(this, msg);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jScrollPane1 = new javax.swing.JScrollPane();
        orderTable = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        orderLineTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        newOrderButton = new javax.swing.JButton();
        editOrderButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        orderTable.setModel(presentation.getOrderTablePresenter());
        jScrollPane1.setViewportView(orderTable);

        orderLineTable.setModel(presentation.getOrderLineTablePresenter());
        jScrollPane2.setViewportView(orderLineTable);

        jLabel1.setText("Orders:");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, orderTable, org.jdesktop.beansbinding.ObjectProperty.create(), jLabel1, org.jdesktop.beansbinding.BeanProperty.create("labelFor"));
        bindingGroup.addBinding(binding);

        jLabel2.setText("Regels:");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, orderLineTable, org.jdesktop.beansbinding.ObjectProperty.create(), jLabel2, org.jdesktop.beansbinding.BeanProperty.create("labelFor"));
        bindingGroup.addBinding(binding);

        newOrderButton.setText("Nieuw");
        newOrderButton.setActionCommand("NewOrder");

        editOrderButton.setText("Wijzig");
        editOrderButton.setActionCommand("EditOrder");
        editOrderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editOrderButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 226, Short.MAX_VALUE)
                        .add(editOrderButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(newOrderButton))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel2)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(newOrderButton)
                        .add(editOrderButton))
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 113, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void editOrderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editOrderButtonActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_editOrderButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton editOrderButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton newOrderButton;
    private javax.swing.JTable orderLineTable;
    private javax.swing.JTable orderTable;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public JTable getOrderLineTable() {
        return orderLineTable;
    }

    public JTable getOrderTable() {
        return orderTable;
    }

    public JButton getNewOrderButton() {
        return newOrderButton;
    }

    public JButton getEditOrderButton() {
        return editOrderButton;
    }
    
}
